#ifndef VIDEO_RENDER_WIDGET_H
#define VIDEO_RENDER_WIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <d3d11.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/hwcontext.h>

class VideoRenderWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    explicit VideoRenderWidget(QWidget *parent = nullptr);
    ~VideoRenderWidget() override;

    bool initializeDecoder(const QString &inputFile);
    void startRendering();
    void stopRendering();

protected:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;

private:
    // D3D11相关
    ID3D11Device* m_d3d11Device = nullptr;
    ID3D11DeviceContext* m_d3d11Context = nullptr;

    // FFmpeg相关
    AVFormatContext* m_fmtCtx = nullptr;
    AVCodecContext* m_codecCtx = nullptr;
    int m_videoStreamIdx = -1;
    AVPacket* m_packet = nullptr;
    AVFrame* m_frame = nullptr;
    bool m_rendering = false;

    // OpenGL相关
    QOpenGLShaderProgram* m_shaderProgram = nullptr;
    GLuint m_vertexArray = 0;
    GLuint m_vertexBuffer = 0;
    GLuint m_texture = 0;
    HANDLE m_wglDevice = nullptr;

    // 初始化D3D11设备
    bool initD3D11();
    
    // 渲染一帧
    void renderFrame(AVFrame* frame);
    
    // 检查WGL扩展支持
    bool checkWglExtensions();
};

#endif // VIDEO_RENDER_WIDGET_H
